package com.cloudknow.healthyRun.activity.common;

import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudknow.activity.BaseActivity;
import com.cloudknow.bean.UserInfoBean;
import com.cloudknow.common.Constant;
import com.cloudknow.healthyRun.R;
import com.cloudknow.utils.PrefsUtil;
import com.yunzhi.net.api.AppApiService;
import com.yunzhi.net.api.NetObserver;
import com.yunzhi.net.entity.BaseResponse;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * Create bu wubin on .
 *
 * @description 角色登录
 */
public class RoleLoginActivity extends BaseActivity implements View.OnClickListener {

    private String TAG = getClass().getSimpleName();

    private ImageView back;
    private EditText phone_no;
    private EditText login_password;
    private CheckBox password_status;
    private CheckBox agreement_1;
    private TextView agreement_2;
    private TextView to_login;

    private String roleName;

    private void initView() {
        back = findViewById(R.id.back);
        phone_no = findViewById(R.id.phone_no);
        login_password = findViewById(R.id.login_password);
        password_status = findViewById(R.id.password_status);
        agreement_1 = findViewById(R.id.agreement_1);
        agreement_2 = findViewById(R.id.agreement_2);
        to_login = findViewById(R.id.to_login);


    }


    @Override
    protected int setView() {
        return R.layout.activity_common_role_login;
    }

    @Override
    protected void receiveData() {
        roleName = PrefsUtil.getString(RoleLoginActivity.this, Constant.isRole);
    }

    @Override
    protected void initializeView() {
        initView();
        initSetOnClick();
        initData();
    }

    private void initData() {
        switch (roleName){
            case Constant.isRole_student:
                phone_no.setHint(getResources().getString(R.string.home_test_info_88));
                break;

            default:
                phone_no.setHint(getResources().getString(R.string.home_test_info_89));
                break;
        }

    }

    private void initSetOnClick() {
        password_status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "onCheckedChanged: " + isChecked);
                if (isChecked) {
                    // 选择状态 显示明文--设置为可见的密码
                    login_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    Editable etext = login_password.getText();
                    Selection.setSelection(etext, etext.length());
                } else {
                    // 默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                    login_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    Editable etext = login_password.getText();
                    Selection.setSelection(etext, etext.length());
                }
            }
        });

        back.setOnClickListener(this);
        to_login.setOnClickListener(this);
    }


    @Override
    protected void loadDataFromServer() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                PrefsUtil.setInt(RoleLoginActivity.this, Constant.isFirstInstall, 2);
                openActivity(RoleChoiceActivity.class);
                finish();
                break;

            case R.id.to_login:
                String role_no = phone_no.getText().toString().trim();
                String role_pass = login_password.getText().toString().trim();
                String role_type = roleName.toString().trim();
//
//
//                if(role_no.isEmpty()||role_pass.isEmpty()){
//                    ToastUtils.showToast(this,getResources().getString(R.string.home_test_info_95));
//                    return;
//                }
//                if(!agreement_1.isChecked()){
//                    ToastUtils.showToast(this,getResources().getString(R.string.home_test_info_96));
//                    return;
//                }
                if(role_type.equals(Constant.isRole_student)){
                    role_type = "1";
                }else {
                    role_type = "2";
                }


//                giveCommon(role_no,role_pass,role_type);
//                giveCommon2(role_no,role_pass,role_type);
                giveCommon3(role_no,role_pass,role_type);

//                giveCommon1("2016211093","123456","1");

                break;

            default:
                break;
        }
    }

    private UserInfoBean userInfoBean;

//    private void giveCommon(String role_no, String role_pass, String role_type) {
//        Map<String, Object> goodsResommendMap = new HashMap<>();
//        goodsResommendMap.put("userCode", "2016211093");
//        goodsResommendMap.put("password", "123456");
//        goodsResommendMap.put("type", "1");
//        Log.w(TAG,goodsResommendMap+"");
//
//        AppApiService.getInstance().initLogin(goodsResommendMap, new NetObserver<BaseResponse<UserInfoBean>>(this,false) {
//            @Override
//            public void disposable(Disposable disposable) {
//                Log.w(TAG,"disposable");
//            }
//
//            @Override
//            public void onSuccess(BaseResponse<UserInfoBean> entity) {
//                userInfoBean = entity.getData();
//                Log.w(TAG,userInfoBean.getAccess_token()+"");
//            }
//
//            @Override
//            public void onError(int code, String msg) {
//                Log.w(TAG,"onError");
//            }
//        });
//    }



    private void giveCommon3(String role_no, String role_pass, String role_type) {
        Map<String, Object> goodsResommendMap = new HashMap<>();
        goodsResommendMap.put("userCode", "2016211093");
        goodsResommendMap.put("password", "123456");
        goodsResommendMap.put("type", "1");
        Log.w(TAG,goodsResommendMap+"");



        AppApiService.getInstance().initLogin3(goodsResommendMap, new NetObserver<BaseResponse<UserInfoBean>>(this,false) {
            @Override
            public void disposable(Disposable disposable) {
                Log.w(TAG,"disposable");
            }

            @Override
            public void onSuccess(BaseResponse<UserInfoBean> entity) {
                userInfoBean = entity.getData();
                Log.w(TAG,userInfoBean.getAccess_token()+"");
            }

            @Override
            public void onError(int code, String msg) {
                Log.w(TAG,"onError");
            }
        });
    }


}
